package com.situ.mall.common.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author yangkx
 * @create 2023-09-11-15:32
 */
public class RequestUtil {

    //封装一个变量，直接定义变量值为固定的值，在所有方法里都可以直接使用变量名
    private static final String CURRENT = "currentUser";

    //获取当前请求
    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

    //获取当前session
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    //把Object对象放进session里
    public static void setCurrent(Object u){
        getSession().setAttribute(CURRENT,u);
    }

    //从session中取出Object对象
    public static Object getCurrent(){
        return getSession().getAttribute(CURRENT);
    }

    //登出——从session中删除对象
}
